<template>
    <!--健康码状态 图表-->
    <div style="height: 100%;">
        <MyChart
            :option="option"
        />
    </div>
</template>

<script>
    import MyChart from '@/components/charts/MyChart.vue';
    import Utils from '@/utils/utils.js';

    export default {
        name: "CodeReqChart",
        props: {
            // 坐标文字、参考线颜色
            labelColor: String,
        },
        components: {
            MyChart,
        },
        data() {
            return {

                codeStatData: [
                    { value: 0, name: '绿码' },
                    { value: 0, name: '黄码' },
                    { value: 0, name: '红码' },
                ],

                // 定时刷新数据任务
                refreshHandle: null,
            }
        },
        computed: {
            option(){
                let {labelColor, codeStatData} = this;

                let option =  {
                    color: ['#37b44a', '#fad81e', '#f34334'],
                    title: {
                        text: '用户健康码分布',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left'
                    },
                    series: [
                        {
                            name: '用户健康码分布',
                            type: 'pie',
                            radius: '50%',
                            data: codeStatData,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                        ]
                }

                if (labelColor) {
                    // 参考分割线颜色
                    option.splitLine = {
                        lineStyle: {
                            color: labelColor
                        }
                    };
                    option.legend.textStyle = {
                        color: "#ffffff"
                    }
                }

                return option;
            },
        },
        created(){
            this.loadStatData();

            this.refreshHandle = setInterval(()=>{
                this.loadStatData();
            }, 5000);
        },
        beforeDestroy(){
            if (this.refreshHandle) {
                clearInterval(this.refreshHandle);
            }
        },
        methods: {
            // 加载统计数据
            loadStatData(){
                this.$api.indexStat.codeColorStat().then(({data}) => {
                    this.codeStatData = data;
                });
            },
        },
    }
</script>

<style scoped>

</style>
